{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4",
      "authorship_tag": "ABX9TyP2KgCcjpZhImtE7dVepHrt",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "53687d7d633941f0a54a6ec53a0db386": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HBoxModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HBoxView",
            "box_style": "",
            "children": [
              "IPY_MODEL_8380e644a56b48c19a42e23bb39a98da",
              "IPY_MODEL_0ce7633498da4978932f3dc6c0b94659",
              "IPY_MODEL_2161e5d9426a416cb212f8107d23dcb5"
            ],
            "layout": "IPY_MODEL_5508d2bfa3454b16ae682d5bfa62b5ff"
          }
        },
        "8380e644a56b48c19a42e23bb39a98da": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_82eebd4dfe764734854070009499bc53",
            "placeholder": "​",
            "style": "IPY_MODEL_024b11dc68434750a3dca9d7be5b75b6",
            "value": "Loading checkpoint shards: 100%"
          }
        },
        "0ce7633498da4978932f3dc6c0b94659": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "FloatProgressModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "ProgressView",
            "bar_style": "success",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_6485161f74c744fd806e405a7a5e722a",
            "max": 2,
            "min": 0,
            "orientation": "horizontal",
            "style": "IPY_MODEL_422e02ef01614a8fb6c9baecb32abe00",
            "value": 2
          }
        },
        "2161e5d9426a416cb212f8107d23dcb5": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_d54db20bb4d64240bea60519f3cc4650",
            "placeholder": "​",
            "style": "IPY_MODEL_c5cb7c55d0684a60a60076459501ba65",
            "value": " 2/2 [01:04&lt;00:00, 29.49s/it]"
          }
        },
        "5508d2bfa3454b16ae682d5bfa62b5ff": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "82eebd4dfe764734854070009499bc53": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "024b11dc68434750a3dca9d7be5b75b6": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "6485161f74c744fd806e405a7a5e722a": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "422e02ef01614a8fb6c9baecb32abe00": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "ProgressStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "bar_color": null,
            "description_width": ""
          }
        },
        "d54db20bb4d64240bea60519f3cc4650": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "c5cb7c55d0684a60a60076459501ba65": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        }
      }
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/LlamaIndex-Tutorials/blob/main/llamaindex_llmlingua_prompt_compression.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Prompt Compression with LlamaIndex and LlmLingua\n",
        "\n",
        "In `RAG` (Retrieval-Augmented Generation), the input tokens consume the most resources, in which a user query is typically sent to a vector storage, to fetch the vector data of the most similar pieces of information. According to the context or relevant documents retrieved from a vector storage by the user's query, the prompt (input) can even reach thousands of tokens.\n",
        "\n",
        "`Prompt compression` is the technique that's used to shorten the original prompt while keeping the most important information, and speed up the language model response generation.\n",
        "\n",
        "The theory it's based on, is that language often includes unnecessary repetition.\n",
        "\n",
        "## LLMLingua\n",
        "\n",
        "https://github.com/microsoft/LLMLingua\n",
        "\n",
        "LLMLingua utilizes a compact, well-trained language model (e.g., GPT2-small, LLaMA-7B) to identify and remove non-essential tokens in prompts. This approach enables efficient inference with large language models (LLMs), achieving up to 20x compression with minimal performance loss."
      ],
      "metadata": {
        "id": "8s5m5rOhvNio"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "26mZc1-BHZBy"
      },
      "outputs": [],
      "source": [
        "!pip install llama-index openai -q -U"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "\n",
        "from google.colab import userdata\n",
        "openai.api_key = userdata.get('OPENAI_API_KEY')"
      ],
      "metadata": {
        "id": "PFObN5uiM_sS"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from llama_index import ServiceContext, set_global_service_context\n",
        "from llama_index.llms import OpenAI\n",
        "from llama_index.callbacks import CallbackManager, TokenCountingHandler\n",
        "import tiktoken\n",
        "\n",
        "OPENAI_MODEL_NAME = \"gpt-3.5-turbo-16k\"\n",
        "\n",
        "llm = OpenAI(model=OPENAI_MODEL_NAME)\n",
        "\n",
        "token_counter = TokenCountingHandler(\n",
        "    tokenizer=tiktoken.encoding_for_model(OPENAI_MODEL_NAME).encode\n",
        ")\n",
        "callback_manager = CallbackManager([token_counter])\n",
        "\n",
        "service_context = ServiceContext.from_defaults(\n",
        "    llm=llm, callback_manager=callback_manager\n",
        ")\n",
        "set_global_service_context(service_context)"
      ],
      "metadata": {
        "id": "JtBt-ucONiNg"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from llama_index import VectorStoreIndex, download_loader\n",
        "\n",
        "WikipediaReader = download_loader(\"WikipediaReader\")\n",
        "loader = WikipediaReader()\n",
        "documents = loader.load_data(pages=['Premier League'])"
      ],
      "metadata": {
        "id": "Vv2TgiiBXS-W"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(len(documents))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GKvzpjsjX-ru",
        "outputId": "91e1c0cf-90ec-4917-ac1c-43953378e2bd"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "retriever = VectorStoreIndex.from_documents(documents).as_retriever(similarity_top_k=3)"
      ],
      "metadata": {
        "id": "3ah1InsfN-Hz"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "question = \"Why did English clubs get banned fron European competition?\""
      ],
      "metadata": {
        "id": "Wtb8jeA6OHlc"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "relevant_documents = retriever.retrieve(question)"
      ],
      "metadata": {
        "id": "UtkLls8JOKxw"
      },
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "contexts = [n.get_content() for n in relevant_documents]\n",
        "contexts"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "s6ATDj-MOREP",
        "outputId": "3c7d9c73-30e7-4e67-b95b-496d88d77ff3"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['== History ==\\n\\n\\n=== Origins ===\\nDespite significant European success in the 1970s and early 1980s, the late 1980s marked a low point for English football. Stadiums were deteriorating and supporters endured poor facilities, hooliganism was rife, and English clubs had been banned from European competition for five years following the Heysel Stadium disaster in 1985. The Football League First Division, the top level of English football since 1888, was behind leagues such as Italy\\'s Serie A and Spain\\'s La Liga in attendances and revenues, and several top English players had moved abroad.By the turn of the 1990s, the downward trend was starting to reverse. At the 1990 FIFA World Cup, England reached the semi-finals; UEFA, European football\\'s governing body, lifted the five-year ban on English clubs playing in European competitions in 1990, resulting in Manchester United lifting the Cup Winners\\' Cup in 1991. The Taylor Report on stadium safety standards, which proposed expensive upgrades to create all-seater stadiums in the aftermath of the Hillsborough disaster, was published in January 1990.During the 1980s, major English clubs had begun to transform into business ventures, applying commercial principles to club administration to maximise revenue. Martin Edwards of Manchester United, Irving Scholar of Tottenham Hotspur, and David Dein of Arsenal were among the leaders in this transformation. The commercial imperative led to the top clubs seeking to increase their power and revenue: the clubs in Division One threatened to break away from the Football League, and in doing so, they managed to increase their voting power and gain a more favourable financial arrangement, taking a 50% share of all television and sponsorship income in 1986. They demanded that television companies should pay more for their coverage of football matches, and revenue from television grew in importance. The Football League received £6.3 million for a two-year agreement in 1986, but by 1988, in a deal agreed with ITV, the price rose to £44 million over four years, with the leading clubs taking 75% of the cash. According to Scholar, who was involved in the negotiations of television deals, each of the First Division clubs received only around £25,000 per year from television rights before 1986, this increased to around £50,000 in the 1986 negotiation, then to £600,000 in 1988. The 1988 negotiations were conducted under the threat of ten clubs leaving to form a \"super league\", but they were eventually persuaded to stay, with the top clubs taking the lion\\'s share of the deal. The negotiations also convinced the bigger clubs that in order to receive enough votes, they needed to take the whole of First Division with them instead of a smaller \"super league\". By the beginning of the 1990s, the big clubs again considered breaking away, especially now that they had to fund the cost of stadium upgrade as proposed by the Taylor Report.In 1990, the managing director of London Weekend Television (LWT), Greg Dyke, met with the representatives of the \"big five\" football clubs in England (Manchester United, Liverpool, Tottenham Hotspur, Everton and Arsenal) over a dinner. The meeting was to pave the way for a breakaway from The Football League. Dyke believed that it would be more lucrative for LWT if only the larger clubs in the country were featured on national television and wanted to establish whether the clubs would be interested in a larger share of television rights money. The five clubs agreed with the suggestion and decided to press ahead with it; however, the league would have no credibility without the backing of The Football Association, and so David Dein of Arsenal held talks to see whether the FA were receptive to the idea. The FA did not have an amicable relationship with the Football League at the time and considered it as a way to weaken the Football League\\'s position. The FA released a report in June 1991, Blueprint for the Future of Football, that supported the plan for the Premier League with the FA as the ultimate authority that would oversee the breakaway league.',\n",
              " '=== Performance in international competition ===\\n\\nWith 48 continental trophies won, English clubs are the third-most successful in European football, behind Italy (49) and Spain (65). In the top-tier UEFA Champions League, a record six English clubs have won a total of 15 titles and lost a further 11 finals, behind Spanish clubs with 19 and 11, respectively. In the second-tier UEFA Europa League, English clubs are also second, with nine victories and eight losses in the finals. In the former second-tier UEFA Cup Winners\\' Cup, English teams won a record eight titles and had a further five finalists. In the non-UEFA organized Inter-Cities Fairs Cup, English clubs provided four winners and four runners-up, the second-most behind Spain with six and three, respectively. In the newly created third-tier UEFA Europa Conference League, English clubs have won a joint-record one title so far. In the former fourth-tier UEFA Intertoto Cup, England won four titles and had a further final appearance, placing it fifth in the rankings, although English clubs were notorious for treating the tournament with disdain, either sending \"B\" squads or withdrawing from it altogether. In the one-off UEFA Super Cup, England has ten winners and ten runners-up, the second-most behind Spain with 16 and 15, respectively. Similarly to the Intertoto Cup, English teams did not take the former Intercontinental Cup seriously enough, despite its international status of the Club World Championship. They a made a total of six appearances in the one-off competition, winning only one of them, and withdrew a further three times. English clubs have won the FIFA-organized Club World Cup four times, tied for the second-most with Brazil, and behind only Spain, with eight.\\n\\n\\n== Sponsorship ==\\n\\nAfter an inaugural season with no sponsorship, the Premier League was sponsored by Carling from 1993 until 2001, during which time it was known as the FA Carling Premiership. In 2001, a new sponsorship deal with Barclaycard saw the league rebranded the FA Barclaycard Premiership, which was changed to the FA Barclays Premiership in time for the 2004–05 season.\\nFor the 2007–08 season, the league was rebranded the Barclays Premier League.\\nBarclays\\' deal with the Premier League expired at the end of the 2015–16 season. The FA announced on 4 June 2015 that it would not pursue any further title sponsorship deals for the Premier League, arguing that they wanted to build a \"clean\" brand for the competition more in line with those of major U.S. sports leagues.\\nAs well as sponsorship for the league itself, the Premier League has a number of official partners and suppliers. The official ball supplier for the league is Nike who have had the contract since the 2000–01 season when they took over from Mitre. Under its Merlin brand, Topps held the licence to produce collectables for the Premier League between 1994 and 2019 including stickers (for their sticker album) and trading cards. Launched in the 2007–08 season, Topps\\' Match Attax, the official Premier League trading card game, is the best selling boys collectable in the UK, and is also the biggest selling sports trading card game in the world. In October 2018, Panini were awarded the licence to produce collectables from the 2019–20 season. The chocolate company Cadbury has been the official snack partner of the Premier League since 2017, and sponsored the Golden Boot, Golden Glove and Playmaker of the Season awards from the 2017–18 season to 2019–20 season. The Coca-Cola Company (under its Coca-Cola Zero Sugar product line) sponsored these awards during the 2020–21 season with Castrol being the current sponsor as of the 2021–22 season.',\n",
              " '=== Foundation (1990s) ===\\n\\nAt the close of the 1990–1991 season, a proposal was tabled for the establishment of a new league that would bring more money into the game overall. The Founder Members Agreement, signed on 17 July 1991 by the game\\'s top-flight clubs, established the basic principles for setting up the FA Premier League. The newly formed top division was to have commercial independence from The Football Association and the Football League, giving the FA Premier League licence to negotiate its own broadcast and sponsorship agreements. The argument given at the time was that the extra income would allow English clubs to compete with teams across Europe. Although Dyke played a significant role in the creation of the Premier League, he and ITV (of which LWT was part) lost out in the bidding for broadcast rights: BSkyB won with a bid of £304 million over five years, with the BBC awarded the highlights package broadcast on Match of the Day.The First Division clubs resigned en masse from the Football League in 1992, and on 27 May that year the FA Premier League was formed as a limited company, working out of an office at the Football Association\\'s then headquarters in Lancaster Gate. The 22 inaugural members of the new Premier League were:\\n\\nThis meant a break-up of the 104-year-old Football League that had operated until then with four divisions; the Premier League would operate with a single division and the Football League with three. There was no change in competition format; the same number of teams competed in the top flight, and promotion and relegation between the Premier League and the new First Division remained the same as the old First and Second Divisions with three teams relegated from the league and three promoted.The league held its first season in 1992–93. It was composed of 22 clubs for that season (reduced to 20 in the 1995–96 season). The first Premier League goal was scored by Brian Deane of Sheffield United in a 2–1 win against Manchester United.Luton Town, Notts County, and West Ham United were the three teams relegated from the old First Division at the end of the 1991–92 season, and did not take part in the inaugural Premier League season.Manchester United won the inaugural edition of the new league, ending a twenty-six year wait to be crowned champions of England. Bolstered by this breakthrough, United immediately became the competition\\'s dominant team, winning seven of the first nine trophies, two League and FA Cup \\'doubles\\' and a European treble, initially under a team of hardened veterans such as Bryan Robson, Steve Bruce, Paul Ince, Mark Hughes and Eric Cantona, before Cantona, Bruce and Roy Keane led a young dynamic new team filled with the Class of 92, a group of young players including David Beckham who came through the Manchester United Academy.\\nBetween 1993 and 1997, Blackburn Rovers and Newcastle United came close to challenging Manchester United\\'s early dominance; Blackburn won the 1994–95 FA Premier League and Newcastle led the title charge over United for much of the 1995–96 season. As the decade closed, Arsenal replicated Manchester United\\'s dominance by winning the League and FA Cup double in 1997–98 and together the \"Big 2\" would form a duopoly over the league between 1997 and 2004.']"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from llama_index.prompts import PromptTemplate\n",
        "\n",
        "template = (\n",
        "    \"Given the context information below: \\n\"\n",
        "    \"---------------------\\n\"\n",
        "    \"{context_str}\"\n",
        "    \"\\n---------------------\\n\"\n",
        "    \"Please answer the question: {query_str}\\n\"\n",
        ")\n",
        "\n",
        "qa_template = PromptTemplate(template)\n",
        "prompt = qa_template.format(context_str=\"\\n\\n\".join(contexts), query_str=question)\n",
        "\n",
        "response = llm.complete(prompt)"
      ],
      "metadata": {
        "id": "sshxLpd-WB44"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response.text"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "0CIih0WpWlJX",
        "outputId": "51f94ed9-5baf-43e2-dc55-59fd51dd1a22"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'English clubs were banned from European competition for five years following the Heysel Stadium disaster in 1985.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\n",
        "    \"Embedding Tokens: \",\n",
        "    token_counter.total_embedding_token_count,\n",
        "    \"\\n\",\n",
        "    \"LLM Prompt Tokens: \",\n",
        "    token_counter.prompt_llm_token_count,\n",
        "    \"\\n\",\n",
        "    \"LLM Completion Tokens: \",\n",
        "    token_counter.completion_llm_token_count,\n",
        "    \"\\n\",\n",
        "    \"Total LLM Token Count: \",\n",
        "    token_counter.total_llm_token_count,\n",
        "    \"\\n\",\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nf30v7hiX0j-",
        "outputId": "a3936047-ef44-443b-9df9-a5da818b4ba3"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Embedding Tokens:  13551 \n",
            " LLM Prompt Tokens:  2363 \n",
            " LLM Completion Tokens:  22 \n",
            " Total LLM Token Count:  2385 \n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install llmlingua accelerate -q -U"
      ],
      "metadata": {
        "id": "Bn1B9Rz4afsp"
      },
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from llama_index.query_engine import RetrieverQueryEngine\n",
        "from llama_index.response_synthesizers import CompactAndRefine\n",
        "from llama_index.indices.postprocessor import LongLLMLinguaPostprocessor\n",
        "node_postprocessor = LongLLMLinguaPostprocessor(\n",
        "    instruction_str=\"Given the context, please answer the final question\",\n",
        "    target_token=300,\n",
        "    rank_method=\"longllmlingua\",\n",
        "    additional_compress_kwargs={\n",
        "        \"condition_compare\": True,\n",
        "        \"condition_in_question\": \"after\",\n",
        "        \"context_budget\": \"+100\",\n",
        "        \"reorder_context\": \"sort\",\n",
        "        \"dynamic_context_compression_ratio\": 0.3,\n",
        "    },\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 253,
          "referenced_widgets": [
            "53687d7d633941f0a54a6ec53a0db386",
            "8380e644a56b48c19a42e23bb39a98da",
            "0ce7633498da4978932f3dc6c0b94659",
            "2161e5d9426a416cb212f8107d23dcb5",
            "5508d2bfa3454b16ae682d5bfa62b5ff",
            "82eebd4dfe764734854070009499bc53",
            "024b11dc68434750a3dca9d7be5b75b6",
            "6485161f74c744fd806e405a7a5e722a",
            "422e02ef01614a8fb6c9baecb32abe00",
            "d54db20bb4d64240bea60519f3cc4650",
            "c5cb7c55d0684a60a60076459501ba65"
          ]
        },
        "id": "tpYopcTlZWX2",
        "outputId": "188b114f-7f83-425c-d0fb-277a8f1593f0"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: \n",
            "The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
            "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
            "You will be able to reuse this secret in all of your notebooks.\n",
            "Please note that authentication is recommended but still optional to access public models or datasets.\n",
            "  warnings.warn(\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]"
            ],
            "application/vnd.jupyter.widget-view+json": {
              "version_major": 2,
              "version_minor": 0,
              "model_id": "53687d7d633941f0a54a6ec53a0db386"
            }
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:381: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.9` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n",
            "  warnings.warn(\n",
            "/usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:386: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.6` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.\n",
            "  warnings.warn(\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "retrieved_nodes = retriever.retrieve(question)\n",
        "synthesizer = CompactAndRefine()"
      ],
      "metadata": {
        "id": "80z15WSjzReI"
      },
      "execution_count": 15,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "retrieved_nodes"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ae8dt9qv54Xu",
        "outputId": "e0ee5f93-fd1d-4e06-aa1b-51656942edc8"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[NodeWithScore(node=TextNode(id_='6ec9417e-decb-4290-a9bf-62630e844bb4', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='28ebd3bd-9540-4e8a-9bff-6976c3aae981', node_type=<ObjectType.DOCUMENT: '4'>, metadata={}, hash='49f783bdfd0ac70d7235553dac9a4b23a646a54a3fa319a87aceffd06b3c1310'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='5f5926b3-c364-4754-b171-a3620c0827d9', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='6a2a1d44721d8e629023dcd71977bcc3bdba201f052ded1ccd93271ba73f4178'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='25597b8b-b1c9-4769-bc2d-ee1bada663af', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='05d3b56d1e30dbad1d491ac9f1dcddeaa4be4f5001d73f5c22c7153de2ede65b')}, hash='38b2bd679b8bd0bcf92b1b878dd1dce93b1e13fb3774cd7ef562e6e1e9236019', text='== History ==\\n\\n\\n=== Origins ===\\nDespite significant European success in the 1970s and early 1980s, the late 1980s marked a low point for English football. Stadiums were deteriorating and supporters endured poor facilities, hooliganism was rife, and English clubs had been banned from European competition for five years following the Heysel Stadium disaster in 1985. The Football League First Division, the top level of English football since 1888, was behind leagues such as Italy\\'s Serie A and Spain\\'s La Liga in attendances and revenues, and several top English players had moved abroad.By the turn of the 1990s, the downward trend was starting to reverse. At the 1990 FIFA World Cup, England reached the semi-finals; UEFA, European football\\'s governing body, lifted the five-year ban on English clubs playing in European competitions in 1990, resulting in Manchester United lifting the Cup Winners\\' Cup in 1991. The Taylor Report on stadium safety standards, which proposed expensive upgrades to create all-seater stadiums in the aftermath of the Hillsborough disaster, was published in January 1990.During the 1980s, major English clubs had begun to transform into business ventures, applying commercial principles to club administration to maximise revenue. Martin Edwards of Manchester United, Irving Scholar of Tottenham Hotspur, and David Dein of Arsenal were among the leaders in this transformation. The commercial imperative led to the top clubs seeking to increase their power and revenue: the clubs in Division One threatened to break away from the Football League, and in doing so, they managed to increase their voting power and gain a more favourable financial arrangement, taking a 50% share of all television and sponsorship income in 1986. They demanded that television companies should pay more for their coverage of football matches, and revenue from television grew in importance. The Football League received £6.3 million for a two-year agreement in 1986, but by 1988, in a deal agreed with ITV, the price rose to £44 million over four years, with the leading clubs taking 75% of the cash. According to Scholar, who was involved in the negotiations of television deals, each of the First Division clubs received only around £25,000 per year from television rights before 1986, this increased to around £50,000 in the 1986 negotiation, then to £600,000 in 1988. The 1988 negotiations were conducted under the threat of ten clubs leaving to form a \"super league\", but they were eventually persuaded to stay, with the top clubs taking the lion\\'s share of the deal. The negotiations also convinced the bigger clubs that in order to receive enough votes, they needed to take the whole of First Division with them instead of a smaller \"super league\". By the beginning of the 1990s, the big clubs again considered breaking away, especially now that they had to fund the cost of stadium upgrade as proposed by the Taylor Report.In 1990, the managing director of London Weekend Television (LWT), Greg Dyke, met with the representatives of the \"big five\" football clubs in England (Manchester United, Liverpool, Tottenham Hotspur, Everton and Arsenal) over a dinner. The meeting was to pave the way for a breakaway from The Football League. Dyke believed that it would be more lucrative for LWT if only the larger clubs in the country were featured on national television and wanted to establish whether the clubs would be interested in a larger share of television rights money. The five clubs agreed with the suggestion and decided to press ahead with it; however, the league would have no credibility without the backing of The Football Association, and so David Dein of Arsenal held talks to see whether the FA were receptive to the idea. The FA did not have an amicable relationship with the Football League at the time and considered it as a way to weaken the Football League\\'s position. The FA released a report in June 1991, Blueprint for the Future of Football, that supported the plan for the Premier League with the FA as the ultimate authority that would oversee the breakaway league.', start_char_idx=2624, end_char_idx=6734, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'), score=0.878433755179298),\n",
              " NodeWithScore(node=TextNode(id_='2570d30c-e15f-4859-9623-a21679e064ac', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='28ebd3bd-9540-4e8a-9bff-6976c3aae981', node_type=<ObjectType.DOCUMENT: '4'>, metadata={}, hash='49f783bdfd0ac70d7235553dac9a4b23a646a54a3fa319a87aceffd06b3c1310'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='ad2559e9-27d9-470d-8743-85bdc486b9a4', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='b50f48d6a7e334808cab9581ed47e2caf0320017ce2a77382d905e465e4a4a70'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='2c71c7b2-eb25-4350-9987-d631a79a2ab1', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='aafe86b41a60bf8a8cda2344bf827a9e4d4d34b0ce86fce663763cac40a3871f')}, hash='9f95728b7f8e639234e64f166776d5363b347a4c38cfe6720eb1a59b232f6150', text='=== Performance in international competition ===\\n\\nWith 48 continental trophies won, English clubs are the third-most successful in European football, behind Italy (49) and Spain (65). In the top-tier UEFA Champions League, a record six English clubs have won a total of 15 titles and lost a further 11 finals, behind Spanish clubs with 19 and 11, respectively. In the second-tier UEFA Europa League, English clubs are also second, with nine victories and eight losses in the finals. In the former second-tier UEFA Cup Winners\\' Cup, English teams won a record eight titles and had a further five finalists. In the non-UEFA organized Inter-Cities Fairs Cup, English clubs provided four winners and four runners-up, the second-most behind Spain with six and three, respectively. In the newly created third-tier UEFA Europa Conference League, English clubs have won a joint-record one title so far. In the former fourth-tier UEFA Intertoto Cup, England won four titles and had a further final appearance, placing it fifth in the rankings, although English clubs were notorious for treating the tournament with disdain, either sending \"B\" squads or withdrawing from it altogether. In the one-off UEFA Super Cup, England has ten winners and ten runners-up, the second-most behind Spain with 16 and 15, respectively. Similarly to the Intertoto Cup, English teams did not take the former Intercontinental Cup seriously enough, despite its international status of the Club World Championship. They a made a total of six appearances in the one-off competition, winning only one of them, and withdrew a further three times. English clubs have won the FIFA-organized Club World Cup four times, tied for the second-most with Brazil, and behind only Spain, with eight.\\n\\n\\n== Sponsorship ==\\n\\nAfter an inaugural season with no sponsorship, the Premier League was sponsored by Carling from 1993 until 2001, during which time it was known as the FA Carling Premiership. In 2001, a new sponsorship deal with Barclaycard saw the league rebranded the FA Barclaycard Premiership, which was changed to the FA Barclays Premiership in time for the 2004–05 season.\\nFor the 2007–08 season, the league was rebranded the Barclays Premier League.\\nBarclays\\' deal with the Premier League expired at the end of the 2015–16 season. The FA announced on 4 June 2015 that it would not pursue any further title sponsorship deals for the Premier League, arguing that they wanted to build a \"clean\" brand for the competition more in line with those of major U.S. sports leagues.\\nAs well as sponsorship for the league itself, the Premier League has a number of official partners and suppliers. The official ball supplier for the league is Nike who have had the contract since the 2000–01 season when they took over from Mitre. Under its Merlin brand, Topps held the licence to produce collectables for the Premier League between 1994 and 2019 including stickers (for their sticker album) and trading cards. Launched in the 2007–08 season, Topps\\' Match Attax, the official Premier League trading card game, is the best selling boys collectable in the UK, and is also the biggest selling sports trading card game in the world. In October 2018, Panini were awarded the licence to produce collectables from the 2019–20 season. The chocolate company Cadbury has been the official snack partner of the Premier League since 2017, and sponsored the Golden Boot, Golden Glove and Playmaker of the Season awards from the 2017–18 season to 2019–20 season. The Coca-Cola Company (under its Coca-Cola Zero Sugar product line) sponsored these awards during the 2020–21 season with Castrol being the current sponsor as of the 2021–22 season.', start_char_idx=30324, end_char_idx=34008, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'), score=0.8618792149943213),\n",
              " NodeWithScore(node=TextNode(id_='25597b8b-b1c9-4769-bc2d-ee1bada663af', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='28ebd3bd-9540-4e8a-9bff-6976c3aae981', node_type=<ObjectType.DOCUMENT: '4'>, metadata={}, hash='49f783bdfd0ac70d7235553dac9a4b23a646a54a3fa319a87aceffd06b3c1310'), <NodeRelationship.PREVIOUS: '2'>: RelatedNodeInfo(node_id='6ec9417e-decb-4290-a9bf-62630e844bb4', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='38b2bd679b8bd0bcf92b1b878dd1dce93b1e13fb3774cd7ef562e6e1e9236019'), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='6e4e797a-5953-44c3-841a-19ed91c4326a', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='de9dbc63008a8f665a208abfdc0ac575cf6e69d56fc356e92a158d4927010539')}, hash='05d3b56d1e30dbad1d491ac9f1dcddeaa4be4f5001d73f5c22c7153de2ede65b', text='=== Foundation (1990s) ===\\n\\nAt the close of the 1990–1991 season, a proposal was tabled for the establishment of a new league that would bring more money into the game overall. The Founder Members Agreement, signed on 17 July 1991 by the game\\'s top-flight clubs, established the basic principles for setting up the FA Premier League. The newly formed top division was to have commercial independence from The Football Association and the Football League, giving the FA Premier League licence to negotiate its own broadcast and sponsorship agreements. The argument given at the time was that the extra income would allow English clubs to compete with teams across Europe. Although Dyke played a significant role in the creation of the Premier League, he and ITV (of which LWT was part) lost out in the bidding for broadcast rights: BSkyB won with a bid of £304 million over five years, with the BBC awarded the highlights package broadcast on Match of the Day.The First Division clubs resigned en masse from the Football League in 1992, and on 27 May that year the FA Premier League was formed as a limited company, working out of an office at the Football Association\\'s then headquarters in Lancaster Gate. The 22 inaugural members of the new Premier League were:\\n\\nThis meant a break-up of the 104-year-old Football League that had operated until then with four divisions; the Premier League would operate with a single division and the Football League with three. There was no change in competition format; the same number of teams competed in the top flight, and promotion and relegation between the Premier League and the new First Division remained the same as the old First and Second Divisions with three teams relegated from the league and three promoted.The league held its first season in 1992–93. It was composed of 22 clubs for that season (reduced to 20 in the 1995–96 season). The first Premier League goal was scored by Brian Deane of Sheffield United in a 2–1 win against Manchester United.Luton Town, Notts County, and West Ham United were the three teams relegated from the old First Division at the end of the 1991–92 season, and did not take part in the inaugural Premier League season.Manchester United won the inaugural edition of the new league, ending a twenty-six year wait to be crowned champions of England. Bolstered by this breakthrough, United immediately became the competition\\'s dominant team, winning seven of the first nine trophies, two League and FA Cup \\'doubles\\' and a European treble, initially under a team of hardened veterans such as Bryan Robson, Steve Bruce, Paul Ince, Mark Hughes and Eric Cantona, before Cantona, Bruce and Roy Keane led a young dynamic new team filled with the Class of 92, a group of young players including David Beckham who came through the Manchester United Academy.\\nBetween 1993 and 1997, Blackburn Rovers and Newcastle United came close to challenging Manchester United\\'s early dominance; Blackburn won the 1994–95 FA Premier League and Newcastle led the title charge over United for much of the 1995–96 season. As the decade closed, Arsenal replicated Manchester United\\'s dominance by winning the League and FA Cup double in 1997–98 and together the \"Big 2\" would form a duopoly over the league between 1997 and 2004.', start_char_idx=6737, end_char_idx=10023, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'), score=0.8386493120583591)]"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from llama_index.indices.query.schema import QueryBundle\n",
        "\n",
        "new_retrieved_nodes = node_postprocessor.postprocess_nodes(\n",
        "    retrieved_nodes, query_bundle=QueryBundle(query_str=question)\n",
        ")"
      ],
      "metadata": {
        "id": "RwBETqYpZZWO"
      },
      "execution_count": 17,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "original_contexts = \"\\n\\n\".join([n.get_content() for n in retrieved_nodes])\n",
        "compressed_contexts = \"\\n\\n\".join([n.get_content() for n in new_retrieved_nodes])\n",
        "original_tokens = node_postprocessor._llm_lingua.get_token_length(original_contexts)\n",
        "compressed_tokens = node_postprocessor._llm_lingua.get_token_length(compressed_contexts)\n",
        "print(compressed_contexts)\n",
        "print()\n",
        "print(\"Original Tokens:\", original_tokens)\n",
        "print(\"Compressed Tokens:\", compressed_tokens)\n",
        "print(\"Compressed Ratio:\", f\"{original_tokens/(compressed_tokens + 1e-5):.2f}x\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WVTBwsFJZeqN",
        "outputId": "e9b30369-cf36-4cbb-b6d0-d2e240affce9"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            " ===Desite significant European success in the 197 and the late9s marked a low for footballs andters poor facilities,ism wasife, and English had been European in5 The First level of English since, wass A attendues, top English moved abroad.By9 theend was reverse9,s; UEFA European, lifted the five-year on English playing in European competitions in19 in Cupinners1 Report on safety, proposed to stadiums the, in09s, major English begun to commercial club administrationvenue Edwards Unitedolarur and were the this transformation The commercial imperative the clubs seeking to increase their power the away from League in so, to increase their voting and gain more arrangement50 ofship income in18. They demanded companies for of football re from in receivedyear in98, but by, deal price leading clubs taking ofash. Sch, whoations ofals each First5 rights800 in6,08.188 the clubs form a \" but were eventually to, top taking theion deal Theations also in receive needed take whole of First Division of a By the90 the big again that had of stad.In the man, withbig) meetingaway from The Football League. Dyke believed that it would be more lucrative for LWT if only the larger clubs in the country were featured on national television and wanted to establish whether the clubs would be interested in a larger share of television rights money. The five clubs agreed with the suggestion and decided to press ahead with it; however, the league would have no credibility without the backing of The Football Association, and so David Dein of Arsenal held talks to see whether the FA were receptive to the idea. The FA did not have an amicable relationship with the Football League at the time and considered it as a way to weaken the Football League's position. The FA released a report in June 1991, Blueprint for the Future of Football, that supported the plan for the Premier League with the FA as the ultimate authority that would oversee the breakaway league.\n",
            "\n",
            "Original Tokens: 2768\n",
            "Compressed Tokens: 443\n",
            "Compressed Ratio: 6.25x\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "token_counter.reset_counts()"
      ],
      "metadata": {
        "id": "yrppFM1z-JJR"
      },
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response = synthesizer.synthesize(question, new_retrieved_nodes)"
      ],
      "metadata": {
        "id": "Snht3Z8SaCeI"
      },
      "execution_count": 20,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "esMKWsrUaD3X",
        "outputId": "1383303c-edb6-4cf8-f191-e9933617488f"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Response(response='English clubs were banned from European competition due to poor facilities, hooliganism, and a history of violence.', source_nodes=[NodeWithScore(node=TextNode(id_='d1b09aff-6ac7-40f0-9ef6-797a03e872e3', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='ea6d1aeb9d0b0098d02bbc88f3338265ea944b0ec66744e065e8296a7dfe1068', text='\\n ===Desite significant European success in the 197 and the late9s marked a low for footballs andters poor facilities,ism wasife, and English had been European in5 The First level of English since, wass A attendues, top English moved abroad.By9 theend was reverse9,s; UEFA European, lifted the five-year on English playing in European competitions in19 in Cupinners1 Report on safety, proposed to stadiums the, in09s, major English begun to commercial club administrationvenue Edwards Unitedolarur and were the this transformation The commercial imperative the clubs seeking to increase their power the away from League in so, to increase their voting and gain more arrangement50 ofship income in18. They demanded companies for of football re from in receivedyear in98, but by, deal price leading clubs taking ofash. Sch, whoations ofals each First5 rights800 in6,08.188 the clubs form a \" but were eventually to, top taking theion deal Theations also in receive needed take whole of First Division of a By the90 the big again that had of stad.In the man, withbig) meetingaway from The Football League. Dyke believed that it would be more lucrative for LWT if only the larger clubs in the country were featured on national television and wanted to establish whether the clubs would be interested in a larger share of television rights money. The five clubs agreed with the suggestion and decided to press ahead with it; however, the league would have no credibility without the backing of The Football Association, and so David Dein of Arsenal held talks to see whether the FA were receptive to the idea. The FA did not have an amicable relationship with the Football League at the time and considered it as a way to weaken the Football League\\'s position. The FA released a report in June 1991, Blueprint for the Future of Football, that supported the plan for the Premier League with the FA as the ultimate authority that would oversee the breakaway league.', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'), score=None)], metadata={'d1b09aff-6ac7-40f0-9ef6-797a03e872e3': {}})"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "response.response"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "DenC2vcNy7Fd",
        "outputId": "210b7d9f-41c0-4120-fca3-f40a74a95bcc"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'English clubs were banned from European competition due to poor facilities, hooliganism, and a history of violence.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(\n",
        "    \"Embedding Tokens: \",\n",
        "    token_counter.total_embedding_token_count,\n",
        "    \"\\n\",\n",
        "    \"LLM Prompt Tokens: \",\n",
        "    token_counter.prompt_llm_token_count,\n",
        "    \"\\n\",\n",
        "    \"LLM Completion Tokens: \",\n",
        "    token_counter.completion_llm_token_count,\n",
        "    \"\\n\",\n",
        "    \"Total LLM Token Count: \",\n",
        "    token_counter.total_llm_token_count,\n",
        "    \"\\n\",\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "p732JJs0-Nz0",
        "outputId": "51613b7c-a3c3-4154-cf84-16de440c6631"
      },
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Embedding Tokens:  0 \n",
            " LLM Prompt Tokens:  525 \n",
            " LLM Completion Tokens:  23 \n",
            " Total LLM Token Count:  548 \n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "retrieved_nodes = retriever.retrieve(question)\n",
        "\n",
        "new_retrieved_nodes = node_postprocessor.postprocess_nodes(\n",
        "    retrieved_nodes, query_bundle=QueryBundle(query_str=question)\n",
        ")\n",
        "\n",
        "contexts = [n.get_content() for n in new_retrieved_nodes]\n",
        "prompt = qa_template.format(context_str=\"\\n\\n\".join(contexts), query_str=question)\n",
        "response = llm.complete(prompt)"
      ],
      "metadata": {
        "id": "N3qQz83U5YEn"
      },
      "execution_count": 24,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "new_retrieved_nodes"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "y6CKc0Zl5wGV",
        "outputId": "51b42d17-a11c-4690-dafa-f202dfdf2811"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[NodeWithScore(node=TextNode(id_='bfc9ecb3-ad7d-4d50-a47f-86ab58af44db', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='ea6d1aeb9d0b0098d02bbc88f3338265ea944b0ec66744e065e8296a7dfe1068', text='\\n ===Desite significant European success in the 197 and the late9s marked a low for footballs andters poor facilities,ism wasife, and English had been European in5 The First level of English since, wass A attendues, top English moved abroad.By9 theend was reverse9,s; UEFA European, lifted the five-year on English playing in European competitions in19 in Cupinners1 Report on safety, proposed to stadiums the, in09s, major English begun to commercial club administrationvenue Edwards Unitedolarur and were the this transformation The commercial imperative the clubs seeking to increase their power the away from League in so, to increase their voting and gain more arrangement50 ofship income in18. They demanded companies for of football re from in receivedyear in98, but by, deal price leading clubs taking ofash. Sch, whoations ofals each First5 rights800 in6,08.188 the clubs form a \" but were eventually to, top taking theion deal Theations also in receive needed take whole of First Division of a By the90 the big again that had of stad.In the man, withbig) meetingaway from The Football League. Dyke believed that it would be more lucrative for LWT if only the larger clubs in the country were featured on national television and wanted to establish whether the clubs would be interested in a larger share of television rights money. The five clubs agreed with the suggestion and decided to press ahead with it; however, the league would have no credibility without the backing of The Football Association, and so David Dein of Arsenal held talks to see whether the FA were receptive to the idea. The FA did not have an amicable relationship with the Football League at the time and considered it as a way to weaken the Football League\\'s position. The FA released a report in June 1991, Blueprint for the Future of Football, that supported the plan for the Premier League with the FA as the ultimate authority that would oversee the breakaway league.', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n'), score=None)]"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "response.text"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 73
        },
        "id": "HWoklTzM5jqQ",
        "outputId": "f46e0749-f852-49a0-92ff-e847f545a11d"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'English clubs were banned from European competition due to a series of incidents and issues in the 1980s. These included poor facilities, hooliganism, and a lack of safety measures in stadiums. The ban was imposed by UEFA, the governing body for European football, in an effort to address these problems and improve the overall image and safety of the sport. The ban lasted for five years, from 1985 to 1990.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    }
  ]
}